package cn.wgx.module.sys.web;

import cn.wgx.module.base.web.BaseController;
import cn.wgx.module.sys.entity .SysUser;
import cn.wgx.module.sys .services.SysUserService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

import static cn.wgx.commons.interceptor.UserHandlerInterceptor.SESSION_USER;

@Api(tags = "")
@RestController
@RequestMapping(path = "api/sysUser")
public class SysUserController extends BaseController {

    @Resource
    SysUserService service;

    @ApiOperation(value = "列表", notes = "" )
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @ModelAttribute SysUser record, 
			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
            @ApiParam(value = "是否倒序,默认是")@RequestParam(defaultValue = "true", required = false)Boolean desc
        ) {
		return returnData(PageHelper.startPage(page, pageSize), service.getList(record, desc));
    }


    @ApiOperation(value = "登录", notes = "" )
    @RequestMapping(path = "login", method = RequestMethod.GET)
    public Map<String, Object> login(@ModelAttribute SysUser sysUser, HttpSession session) {
        SysUser login = service.login(sysUser);
        if(login != null){
            session.setAttribute(SESSION_USER, login);
            return returnData(login);
        }else{
            return returnErrorMap("账户密码错误");
        }
    }

    @ApiOperation(value = "退出登录", notes = "" )
    @RequestMapping(path = "loginOut", method = RequestMethod.GET)
    public Map<String, Object> out(HttpSession session) {
        session.setAttribute(SESSION_USER, null);
        return returnData(true);

    }

    @ApiOperation(value = "检测登录", notes = "" )
    @RequestMapping(path = "getSession", method = RequestMethod.GET)
    public Map<String, Object> getSession(HttpSession session) {
        Object attribute = session.getAttribute(SESSION_USER);
        if(attribute != null){
            return returnData(attribute);
        }else{
            return returnErrorMap("未登录");
        }
    }

	
    @ApiOperation(value = "新建", notes = "必须参数: " )
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody SysUser record) {
        return returnData(service.insertOne(record));
    }

	
    @ApiOperation(value = "修改", notes = "" )
    @RequestMapping(path = "updatePass", method = RequestMethod.PATCH)
    public Map<String, Object> updateOne(@RequestBody SysUser record, HttpSession session) {
        Object attribute = session.getAttribute(SESSION_USER);
        if(attribute instanceof SysUser){
            SysUser sysUser = (SysUser)attribute;
            if(sysUser.getPwd().equals(record.getPwd())){
                SysUser record2 = new SysUser();
                record2.setPwd(record.getUsername());
                record2.setUsername(sysUser.getUsername());
                int n = service.updatePass(record2);
                if(n > 0){
                    session.setAttribute(SESSION_USER, null);
                    return returnData(n);
                }
            }
        }

        return returnErrorMap("失败");
    }


//    @ApiOperation(value = "删除", notes = "" )
//    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
//    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
//        return returnData(service.deleteOne(id));
//    }

	

}